package cur.nature.admin.service;

import cur.nature.admin.dao.Admin_MidDao;
import cur.nature.admin.dao.Admin_RoleDao;
import cur.nature.admin.model.po.Admin_Mid_RoleResourcePO;
import cur.nature.admin.model.po.Admin_RolePO;
import cur.nature.admin.model.vo.Admin_RoleVO;
import cur.nature.framework.core.mirco.MircoIdUtil;
import cur.nature.framework.core.util.IdUtil;
import cur.nature.framework.core.util.ObjectUtil;
import cur.nature.framework.core.util.SnowflakeIdUtil;
import cur.nature.framework.core.util.StringUtil;
import cur.nature.framework.mvc.crud.CrudService;
import cur.nature.framework.mvc.tree.TreeNode;
import cur.nature.framework.mvc.tree.TreeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户角色 Service
 * 作者：Auto Generator By wujian
 * 生成日期：2023-07-10 21:42:14
 */
@Service
public class Admin_RoleService extends CrudService<Admin_RolePO, Admin_RoleVO, Admin_RoleDao> {
    public Admin_RoleService(Admin_RoleDao baseDao) {
        super(baseDao, Admin_RolePO.class, Admin_RoleVO.class);
    }

    @Autowired
    private Admin_MidDao midDao;

    public Admin_RoleVO.list_Result list(Admin_RoleVO.list_Param param) throws Exception {
        Admin_RoleVO.list_Result result = new Admin_RoleVO.list_Result();
        TreeNode topNode = new TreeNode("0", param.getNodeText());
        result.setTreeNode(topNode);
        // 查询出所有的资源
        List<Admin_RoleVO> list = getDao().queryByCond(param);
        if (ObjectUtil.isNotEmpty(list)) {
            // 查出资源中间表数据
            List<String> roleIdList = list.stream().map(v->v.getId()+"").collect(Collectors.toList());
            List<Admin_Mid_RoleResourcePO> resList = midDao.queryRoleResource(roleIdList);

            // 设置资源id
            if (resList != null) {
                for (Admin_RoleVO base : list) {
                    List<String> resIdList = resList.stream().filter(s -> StringUtil.equals(base.getId()+"", s.getRoleId()))
                            .map(Admin_Mid_RoleResourcePO::getResourceId).toList();
                    base.setResId(resIdList);
                }
            }

            //转换为树节点
            List<TreeNode> treeNodeList = list.stream().map(base -> new TreeNode(base.getId() + "", base.getParentId(), base.getName(), base))
                    .collect(Collectors.toList());
            //组装树
            TreeUtil.packTree(treeNodeList, topNode);
        }
        return result;
    }

    @Transactional
    public int operate(List<Admin_RoleVO> param) throws Exception {
        if (param == null || param.size() == 0) {
            return 0;
        }
        int rows = this.saveList(param);
        // 更新中间表
        for (Admin_RoleVO base : param) {
            // 先清除相关数据
            if (StringUtil.isNotEmpty(base.getId()+"")) {
                midDao.deletRoleResourceByRoleId(base.getId());
            }
            // 插入新数据, 新增，修改
            if (base.getOperateType() != 3 && base.getResId() != null) {
                // 插入新数据
                List<Admin_Mid_RoleResourcePO> list = new ArrayList<>();
                for (String resId : base.getResId()) {
                    Admin_Mid_RoleResourcePO po = new Admin_Mid_RoleResourcePO();
                    po.setCreateBase();
                    po.setId(MircoIdUtil.generateId());
                    po.setRoleId(base.getId()+"");
                    po.setResourceId(resId);
                    list.add(po);
                }
                getTemplate().getTemplateProxy().batchInsert(list);
            }
            rows++;
        }
        return rows;
    }

    public List<Admin_RoleVO> querByUserId(Long userId) throws Exception {
        List<Admin_RoleVO> roleVO = this.getDao().queryByUserId(userId);
        return roleVO;
    }
}

